package org.jmc.refmanager.util;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;

@Root(name="attributes")
public class Update implements PropertyChangeListener, Serializable {
	private static final long serialVersionUID = 1L;
	@ElementMap(entry="attribute", key="name", attribute=true, inline=true)
	private Map<String, Object> afterValues = new HashMap<String, Object>();;
	private Map<String, Object> beforeValues = new HashMap<String, Object>();;
	
	public Update() {
		super();
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent event) {
		afterValues.put(event.getPropertyName(), event.getNewValue());
		beforeValues.put(event.getPropertyName(), event.getOldValue());
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return String.format("BeanChanges [changes=%s]", afterValues);
	}

	/**
	 * 
	 * @return
	 */
	public boolean isEmpty() {
		return ListUtil.isEmptyOrNull(afterValues);
	}

	public Object getBeforeValue(String attrName) {
		return beforeValues.get(attrName);
	}
	
	/**
	 * 
	 * @return
	 */
	public Map<String, Object> getAttributeUpdates() {
		return Collections.unmodifiableMap(afterValues);
	}
}
